<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Repositories\ProjectsRepository;
use App\Http\Requests\CreateProjectRequest;
use App\Http\Requests\UpdateProjectRequest;
use App\Project;
class ProjectsController extends Controller
{   
    protected $repo;
    public function __construct(ProjectsRepository $repo){
        $this->repo=$repo;
        $this->middleware('auth');
    }
    // 增(create)
    public function create(){
        
    }
    public function store(CreateProjectRequest $request){
        $this->repo->create($request);
        
       return back();
    }
    // public function destory($id){
    //     Project::findOrfail($id)->delete();
    //     return back();
    // }
   // 删(delete)
    public function destroy($id){
        $this->repo->delete($id);
        return back();
    }
    // 改(update)
    public function edit(){
        
    }
    public function update(UpdateProjectRequest $request , $id){
        $this->repo->update($request,$id);
        return back();
    }
    // 查(show)
    public function index(){
        $projects=$this->repo->list();
        return view('welcome',compact('projects'));
    }
    public function show(Project $project){
        // $project=$this->repo->find($id);
        $todos=$this->repo->todos($project);
        $done=$this->repo->done($project);
        $projects=request()->user()->projects()->pluck('name','id');
        return view('projects.show',compact('project','todos','done','projects'));
    }
    
}
